package net.maku.jw.controller;

import net.maku.framework.common.utils.Result;
import net.maku.jw.entity.VxHelp;
import net.maku.jw.service.VxHelpService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.annotation.Resource;

/**
 * 帮助信息  求助(VxHelp)控制层
 *
 * @author makejava
 * @since 2024-04-05 14:16:57
 */
@RestController
@RequestMapping("/vxHelp")
public class VxHelpController {
    /**
     * 服务对象
     */
    @Resource
    private VxHelpService vxHelpService;

    /**
     * 通过主键查询单条数据
     *
     * @param vxHelp 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public Result<VxHelp> selectOne(VxHelp vxHelp) {
        VxHelp result = vxHelpService.selectById(vxHelp.getId());
        if (result != null) {
            return Result.ok(result);
        }
        return Result.error("查询失败");
    }

    /**
     * 新增一条数据
     *
     * @param vxHelp 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Result<VxHelp> insert(@RequestBody VxHelp vxHelp) {
        int result = vxHelpService.insert(vxHelp);
        if (result > 0) {
            vxHelp.setId(vxHelpService.selectLastId());
            return Result.ok(vxHelp);
        }
        return Result.error("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param vxHelp 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public Result<VxHelp> update(@RequestBody VxHelp vxHelp) {
        VxHelp result = vxHelpService.update(vxHelp);
        if (result != null) {
            return Result.ok(result);
        }
        return Result.error("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param id 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public Result<Integer> delete(Integer id) {
        int result = vxHelpService.deleteById(id);
        if (result > 0) {
            return Result.ok(result);
        }
        return Result.error("删除失败");
    }

    /**
     * 批量删除数据
     *
     * @param ids 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "deletemultiple", method = RequestMethod.DELETE)
    public Result<Integer> deletemultiple(Integer[] ids) {
        int result = vxHelpService.deletemultiple(ids);
        if (result > 0) {
            return Result.ok(result);
        }
        return Result.error("删除失败");
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public Result<List<VxHelp>> selectAll() {
        List<VxHelp> vxHelps = vxHelpService.selectAll();
        if (vxHelps != null) {
            return Result.ok(vxHelps);
        }
        return Result.error("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public Result<List> selectPage(Integer start, Integer limit) {
        start = (start - 1) * limit;
        List<VxHelp> vxHelps = vxHelpService.selectPage(start, limit);
        if (vxHelps != null) {
            return Result.ok(vxHelps);
        }
        return Result.error("查询失败");
    }

    @RequestMapping(value = "selectList", method = RequestMethod.POST)
    public Result<List> selectPage(@RequestBody Map<String, Object> data) {
        System.out.println("data=" + data);


        VxHelp vxHelp = null;
        if (data.get("vxHelp") != null) {
            // 将数据转成json字符串
            String jsonObject = JSON.toJSONString(data.get("vxHelp"));
            //将json转成需要的对象
            vxHelp = JSONObject.parseObject(jsonObject, VxHelp.class);
        }

        Integer start = null;
        Integer limit = null;
        if (data.get("start") != null && data.get("limit") != null) {
            start = (Integer) data.get("start");
            limit = (Integer) data.get("limit");
            start = (start - 1) * limit;
        }
        List<VxHelp> vxHelps = vxHelpService.selectList(vxHelp, start, limit);
        if (vxHelps != null) {
            return Result.ok(vxHelps);
        }
        return Result.error("查询失败");
    }
}




